package com.qihetimes.earthangel.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qihetimes.earthangel.mapper.GoodsPhotoMapper;
import com.qihetimes.earthangel.model.GoodsPhoto;
import com.qihetimes.earthangel.service.IGoodsPhotoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 物品照片表 服务实现类
 * </p>
 *
 * @author f4cklangzi@gmail.com
 * @since 2021-12-11
 */
@Service
public class GoodsPhotoServiceImpl extends ServiceImpl<GoodsPhotoMapper, GoodsPhoto> implements IGoodsPhotoService {
    
    @Override
    public void setGoodsPhotos(String goodsId, List<String> photoUrlList) {
        List<GoodsPhoto> goodsPhotoList = photoUrlList.stream().map(url -> {
            GoodsPhoto goodsPhoto = new GoodsPhoto();
            goodsPhoto.setId(IdUtil.objectId());
            goodsPhoto.setGoodsId(goodsId);
            goodsPhoto.setPhotoUrl(url);
            return goodsPhoto;
        }).collect(Collectors.toList());
        lambdaUpdate().eq(GoodsPhoto::getGoodsId, goodsId).remove();
        saveBatch(goodsPhotoList);
    }
}
